package com.baidu.service.impl;

import com.baidu.entity.ProjectClassEntity;
import com.baidu.mapper.ProjectClassMapper;
import com.baidu.service.ProjectClassService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProjectClassServiceImpl implements ProjectClassService {

    @Override
    public ProjectClassEntity selectProClassById(Long id)  {
        try {
            if (id > 0) {
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

                try (SqlSession session = sqlSessionFactory.openSession()) {
                    // 没有将Mapper改为接口时的写法,从来不会这么用,临时举例而已
                    // ProjectClassEntity res = session.selectOne("com.baidu.mapper.ProjectClassMapper.selectProClassById");

                    // 获取对应的ProjectClassMapper 接口的代理对象.实际开发的用法
                    ProjectClassMapper mapper = session.getMapper(ProjectClassMapper.class);


                    ProjectClassEntity res = mapper.selectProClassById(id, 10);

                    System.out.println("测试结果1");
                    System.out.println(res.toString());
                    System.out.println("测试结果2");

                }
            }
        } catch (IOException e) {
            System.out.println("参数异常");
        }
        return null;
    }

    @Override
    public List<ProjectClassEntity> selectProClassList(ProjectClassEntity entity)  {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            try (SqlSession session = sqlSessionFactory.openSession()) {
                // 没有将Mapper改为接口时的写法,从来不会这么用,临时举例而已
                // ProjectClassEntity res = session.selectOne("com.baidu.mapper.ProjectClassMapper.selectProClassById");

                // 获取对应的ProjectClassMapper 接口的代理对象.实际开发的用法
                ProjectClassMapper mapper = session.getMapper(ProjectClassMapper.class);


                List<ProjectClassEntity> res = mapper.selectProClassList(entity);

                System.out.println("测试结果1");
                System.out.println(res.toString());
                System.out.println("测试结果2");

                return res;
            }
        } catch (IOException e) {
            System.out.println("参数异常");
        }
        return new ArrayList<>();
    }
    @Override
    public List<ProjectClassEntity> selectProClassAndStuList(ProjectClassEntity entity)  {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            try (SqlSession session = sqlSessionFactory.openSession()) {
                // 没有将Mapper改为接口时的写法,从来不会这么用,临时举例而已
                // ProjectClassEntity res = session.selectOne("com.baidu.mapper.ProjectClassMapper.selectProClassById");

                // 获取对应的ProjectClassMapper 接口的代理对象.实际开发的用法
                ProjectClassMapper mapper = session.getMapper(ProjectClassMapper.class);


                List<ProjectClassEntity> res = mapper.selectProClassAndStuList(entity);

                System.out.println("测试结果1");
                System.out.println(res.toString());
                System.out.println("测试结果2");

                return res;
            }
        } catch (IOException e) {
            System.out.println("参数异常");
        }
        return new ArrayList<>();
    }

    @Override
    public int updateProClassById(Long id) {
        try  {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession(false);
             Connection conn = session.getConnection();
             conn.setAutoCommit(false); // 事務不會自動提交
            // 没有将Mapper改为接口时的写法,从来不会这么用,临时举例而已
            // ProjectClassEntity res = session.selectOne("com.baidu.mapper.ProjectClassMapper.selectProClassById");

            // 获取对应的ProjectClassMapper 接口的代理对象.实际开发的用法
            ProjectClassMapper mapper = session.getMapper(ProjectClassMapper.class);

            ProjectClassEntity entity = new ProjectClassEntity();
            entity.setId(id);
            entity.setName("新的项目组名字222221");
            entity.setStuCount(2233322);
//            int row = mapper.updateProClassById(entity, entity);
            // int row = mapper.updateProClassById2(entity);
            int row = mapper.updateProClassById3(entity);
            try {
                session.commit();
                String a = null;
//                a.trim();
            } catch (Exception e) {
                System.out.println("捕获异常=====");
                session.rollback(); // 事物回滚
            } finally {
                session.close();
            }
            return row;
        } catch (Exception e) {
            System.out.println("捕獲異常");
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteProClassById(int[] arr) {
        try  {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            // 没有将Mapper改为接口时的写法,从来不会这么用,临时举例而已
            // ProjectClassEntity res = session.selectOne("com.baidu.mapper.ProjectClassMapper.selectProClassById");

            // 获取对应的ProjectClassMapper 接口的代理对象.实际开发的用法
            ProjectClassMapper mapper = session.getMapper(ProjectClassMapper.class);

            int row = mapper.deleteProClassById(arr);
            session.commit();
            session.close();
            return row;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
